package com.zrh.jsd.service.studydemo.common_thread;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

class SafeDateFormat {
    // 定义 ThreadLocal 变量
    static final ThreadLocal<DateFormat>
            tl = ThreadLocal.withInitial(
            () -> new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss"));

    static DateFormat get() {
        return tl.get();
    }

    public static void main(String[] args) {
        ThreadLocal threadLocal = new ThreadLocal();
        threadLocal.set("123");

        ThreadLocal threadLocal1 = new ThreadLocal();
        threadLocal1.set("546");
        System.out.println(threadLocal.get());
        System.out.println(threadLocal1.get());
        System.out.println(threadLocal);
        System.out.println(threadLocal1);
    }
}